package com.me.utils;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;


/**
 * Description:本工具类已过期，请使用DBUtil <br>
 */
@Deprecated
public class ConnectionUtils {
    private static String driver = null;
    private static String url = null;
    private static String username = null;
    private static String password = null;

    private static ThreadLocal<Connection> tl = new ThreadLocal<Connection>();

    static {
        try {
            driver = IOUtility.getProperty("jdbc.driver");
            url = IOUtility.getProperty("jdbc.url");
            username = IOUtility.getProperty("jdbc.username");
            password = IOUtility.getProperty("jdbc.password");

            Class.forName(driver);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static Connection getConn() throws Exception {
        Connection conn = tl.get();

        if (conn == null) {
            conn = DriverManager.getConnection(url, username, password);
            tl.set(conn);
        }

        return conn;
    }

    public static void closeConn() throws Exception {
        Connection conn = tl.get();
        if (conn != null){
            conn.close();
        }
        tl.set(null);
    }

    public static void main(String[] args) throws SQLException {
        try {
            Connection conn = getConn();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
